package chen.web.message;

import java.util.Date;
import java.util.List;

import chen.web.MyContext;
import chen.web.MyContextProvider;
import chen.web.user.Account;
import chen.web.user.NoPermissionException;
import chen.web.user.PermissionConfigRepository;

public class SecurityMessageManager implements MessageManager {

	private MessageManager mm;
	
	
	private MyContext context(){
		return MyContextProvider.getMyContext();
	}
	
	public SecurityMessageManager(MessageManager manager){
		this.mm = manager;
	}
	
	private MessagePermissionConfig config(){
		return PermissionConfigRepository.loadCurrent(MessagePermissionConfig.class);
	}
	
	@Override
	public void delete(Message message) {
		// TODO Auto-generated method stub
		Account cua = context().getAccount();
		if(message.getReceiver().equals(cua) || message.getSender().equals(cua)){
			mm.delete(message);	
		} else {
			throw new NoPermissionException();	
		}
	}

	@Override
	public void fill(Outbox outbox) {
		// TODO Auto-generated method stub
		if(!outbox.getBelong().equals(context().getAccount())){
			throw new NoPermissionException();	
		}
		mm.fill(outbox);
	}

	@Override
	public void fill(Inbox inbox) {
		// TODO Auto-generated method stub
		if(!inbox.getBelong().equals(context().getAccount())){
			throw new NoPermissionException();	
		}
		mm.fill(inbox);
	}

	@Override
	public Message getMessage(int messageId) {
		// TODO Auto-generated method stub
		Message message = mm.getMessage(messageId);
		Account cua = context().getAccount();
		if(message.getReceiver().equals(cua) || message.getSender().equals(cua)){
			return message;
		} else {
			throw new NoPermissionException();	
		}
	}

	@Override
	public List<Message> getRecord(Account acc1, Account acc2, Date before,
			int beginOffset, int count) {
		// TODO Auto-generated method stub
		Account cua = context().getAccount();
		if(acc1.equals(cua) || acc2.equals(cua)){
			return mm.getRecord(acc1, acc2, before, beginOffset, count);
		} else {
			throw new NoPermissionException();	
		}
	}

	@Override
	public void hasRead(Message message) {
		// TODO Auto-generated method stub
		if(!message.getReceiver().equals(context().getAccount())){
			throw new NoPermissionException();	
		}
		mm.hasRead(message);
	}

	@Override
	public List<Message> listNewMessage(Account account, int beginOffset,
			int count) {
		// TODO Auto-generated method stub
		if(!account.equals(context().getAccount())){
			throw new NoPermissionException();	
		}
		return mm.listNewMessage(account, beginOffset, count);
	}

	@Override
	public int newMessageCount(Account acc) {
		// TODO Auto-generated method stub
		if(!acc.equals(context().getAccount())){
			throw new NoPermissionException();	
		}
		return mm.newMessageCount(acc);
	}

	@Override
	public void send(Message message) {
		// TODO Auto-generated method stub
		if(!config().isSendMessage()){
			throw new NoPermissionException();	
		}
		mm.send(message);	
	}

	@Override
	public int totalReceived(Account acc) {
		// TODO Auto-generated method stub
		if(!acc.equals(context().getAccount())){
			throw new NoPermissionException();	
		}
		return mm.totalReceived(acc);
	}

	@Override
	public int totalSent(Account acc) {
		// TODO Auto-generated method stub
		if(!acc.equals(context().getAccount())){
			throw new NoPermissionException();	
		}
		return mm.totalSent(acc);
	}

}
